﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Windows.Forms;

namespace DotNet.WinForm.Organize
{
    using DotNet.Model;
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// FrmOrganizeEdit.cs
    /// 组织机构管理-添加组织机构窗体
    ///
    /// 修改记录
    ///
    ///     2010.10.21 版本：1.6 DotNet  部门类别进行优化。
    ///     2007.06.05 版本：1.7 DotNet  整理主键。
    ///     2007.05.30 版本：1.4 DotNet  提示信息优化，标准工程完成。
    ///     2007.05.30 版本：1.3 DotNet  整体整理主键
    ///     2007.05.17 版本：1.2 DotNet  增加了多国语言功能，调整了细节部分。
    ///     2007.05.14 版本：1.1 DotNet  改进CheckInput()，SaveEntity()。
    ///     2007.05.10 版本：1.0 DotNet  组织机构添加功能页面编写。
    ///
    /// 版本：1.6
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2010.10.21</date>
    /// </author>
    /// </summary>
    public partial class FrmOrganizeEdit : BaseForm, IBaseOrganizeEditForm
    {
        /// <summary>
        /// 权限域编号
        /// </summary>
        private string permissionScopeCode = "SystemAdmin";

        /// <summary>
        /// 实体
        /// </summary>
        private BaseOrganizeEntity organizeEntity = null;

        public FrmOrganizeEdit()
        {
            InitializeComponent();
        }

        public FrmOrganizeEdit(string entityId)
            : this()
        {
            this.EntityId = entityId;
        }

        #region private void BindItemDetails() 绑定下拉筐数据

        /// <summary>
        /// 绑定下拉筐数据
        /// </summary>
        private void BindItemDetails()
        {
            DataTable dataTable = new DataTable(BaseItemDetailsTable.TableName);
            dataTable = ServiceManager.Instance.ItemDetailsService.GetDTByCode(UserInfo, "OrganizeCategory");

            // 添加一个空记录
            DataRow dataRow = dataTable.NewRow();
            dataTable.Rows.InsertAt(dataRow, 0);

            // 绑定数据
            this.cmbCategory.DisplayMember = BaseItemDetailsTable.FieldItemName;
            this.cmbCategory.ValueMember = BaseItemDetailsTable.FieldItemCode;

            this.cmbCategory.DataViewSource = dataTable.DefaultView;
        }

        #endregion private void BindItemDetails() 绑定下拉筐数据

        #region public override void ShowEntity() 显示内容

        /// <summary>
        /// 显示内容
        /// </summary>
        public override void ShowEntity()
        {
            // 将类转显示到页面
            this.ucParent.SelectedId = organizeEntity.ParentId.ToString();

            //季俊武20110219修改OpenId赋值的条件
            //if (String.IsNullOrEmpty(this.ucParent.SelectedId))
            //{
            this.ucParent.OpenId = organizeEntity.Id.ToString();

            //}
            this.txtCode.Text = organizeEntity.Code;
            this.txtFullName.Text = organizeEntity.FullName;
            this.ucManager.SelectedId = organizeEntity.Manager;
            this.ucManager.ByPermissionCode = this.permissionScopeCode;
            this.ucAssistantManager.SelectedId = organizeEntity.AssistantManager;
            this.ucAssistantManager.ByPermissionCode = this.permissionScopeCode;
            if (organizeEntity.Category != null)
            {
                this.cmbCategory.SelectedValue = organizeEntity.Category;
            }
            this.txtOuterPhone.Text = organizeEntity.OuterPhone;
            this.txtInnerPhone.Text = organizeEntity.InnerPhone;
            this.txtFax.Text = organizeEntity.Fax;
            this.txtPostalcode.Text = organizeEntity.Postalcode;
            this.txtAddress.Text = organizeEntity.Address;
            this.txtWeb.Text = organizeEntity.Web;
            this.chkEnabled.Checked = organizeEntity.Enabled == 1;
            this.chkIsInnerOrganize.Checked = organizeEntity.IsInnerOrganize == 1;
            this.txtDescription.Text = organizeEntity.Description;

            // 这里需要进行判断，数据是否被其他人已经删除
            if (organizeEntity.Id == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.MSG0005, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        #endregion public override void ShowEntity() 显示内容

        #region public override void SetControlState() 设置按钮状态

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        public override void SetControlState()
        {
            this.ucParent.CheckMove = true;
            this.ucParent.OpenId = this.EntityId;

            // 若不是系统管理员，不能添加跟节点
            this.lblParentReq.Visible = false;
            this.ucParent.AllowNull = true;
            if (!UserInfo.IsAdministrator)
            {
                this.lblParentReq.Visible = true;
                this.ucParent.AllowNull = false;
                this.ucParent.ByPermission = this.permissionScopeCode;
            }
        }

        #endregion public override void SetControlState() 设置按钮状态

        #region public override void FormOnLoad() 加载窗体

        /// <summary>
        /// 加载窗体
        /// </summary>
        public override void FormOnLoad()
        {
            // 组织机构信息
            this.organizeEntity = ServiceManager.Instance.OrganizeService.GetEntity(UserInfo, this.EntityId);

            // 绑定下拉筐数据
            this.BindItemDetails();

            // 显示内容
            this.ShowEntity();

            // 设置焦点
            this.ActiveControl = this.txtFullName;
            this.txtCode.Focus();
        }

        #endregion public override void FormOnLoad() 加载窗体

        private void SetCompany(string parentId)
        {
            // 设置部门与公司的联动功能
            this.ucManager.OpenId = parentId;
        }

        #region public override bool CheckInput() 检查输入的有效性

        /// <summary>
        /// 检查输入的有效性
        /// </summary>
        /// <returns>有效</returns>
        public override bool CheckInput()
        {
            bool returnValue = true;

            // 部门是否已经是自己的子部门了？
            this.txtFullName.Text = this.txtFullName.Text.TrimEnd();
            if (this.txtFullName.Text.Trim().Length == 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, AppMessage.MSG9978), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtFullName.Focus();
                return false;
            }

            //if (this.cmbCategory.Text.Trim().Length == 0)
            //{
            //   DevExpress.XtraEditors.XtraMessageBox.Show("分类不能为空。", AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    this.cmbCategory.Focus();
            //    return false;
            //}
            return returnValue;
        }

        #endregion public override bool CheckInput() 检查输入的有效性

        #region private BaseOrganizeEntity GetEntity() 转换数据

        /// <summary>
        /// 转换数据
        /// </summary>
        private BaseOrganizeEntity GetEntity()
        {
            if (string.IsNullOrEmpty(this.ucParent.SelectedId))
            {
                organizeEntity.ParentId = null;
            }
            else
            {
                organizeEntity.ParentId = int.Parse(this.ucParent.SelectedId);
            }
            organizeEntity.Code = this.txtCode.Text;
            organizeEntity.FullName = this.txtFullName.Text;
            if (this.cmbCategory.SelectedValue == null)
            {
                organizeEntity.Category = string.Empty;
            }
            else
            {
                organizeEntity.Category = this.cmbCategory.SelectedValue.ToString();
            }
            organizeEntity.Manager = this.ucManager.SelectedId;
            organizeEntity.AssistantManager = this.ucAssistantManager.SelectedId;
            organizeEntity.OuterPhone = this.txtOuterPhone.Text;
            organizeEntity.InnerPhone = this.txtInnerPhone.Text;
            organizeEntity.Fax = this.txtFax.Text;
            organizeEntity.Postalcode = this.txtPostalcode.Text;
            organizeEntity.Web = this.txtWeb.Text;
            organizeEntity.Enabled = this.chkEnabled.Checked ? 1 : 0;
            organizeEntity.IsInnerOrganize = this.chkIsInnerOrganize.Checked ? 1 : 0;
            organizeEntity.Description = this.txtDescription.Text;
            return organizeEntity;
        }

        #endregion private BaseOrganizeEntity GetEntity() 转换数据

        #region public override bool SaveEntity() 保存

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>保存成功</returns>
        public override bool SaveEntity()
        {
            bool returnValue = false;

            // 转换数据
            this.GetEntity();
            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            ServiceManager.Instance.OrganizeService.Update(UserInfo, this.organizeEntity, out statusCode, out statusMessage);
            if (statusCode == StatusCode.OKUpdate.ToString())
            {
                if (BaseSystemInfo.ShowInformation)
                {
                    // 添加成功，进行提示
                    DevExpress.XtraEditors.XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.DialogResult = DialogResult.OK;
                returnValue = true;
            }
            else
            {
                // 是否名称重复了，提高友善性
                if (statusCode == StatusCode.ErrorNameExist.ToString())
                {
                    this.txtFullName.SelectAll();
                    this.txtFullName.Focus();
                }
                else
                {
                    // 是否编号重复了，提高友善性
                    if (statusCode == StatusCode.ErrorCodeExist.ToString())
                    {
                        this.txtCode.SelectAll();
                        this.txtCode.Focus();
                    }
                }
            }

            // 设置鼠标默认状态
            this.Cursor = Cursors.Default;
            return returnValue;
        }

        #endregion public override bool SaveEntity() 保存

        private void btnSave_Click(object sender, EventArgs e)
        {
            // 检查输入的有效性
            if (this.CheckInput())
            {
                // 设置鼠标繁忙状态
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        // 关闭窗口
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默认状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}